package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.vo.FileVo;
import com.ruoyi.system.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;

/**
 * 文件控制层
 */
@RestController
@RequestMapping("file")
public class SysFileController {
    private final ISysFileService iSysFileService;

    @Autowired
    public SysFileController(ISysFileService iSysFileService) {
        this.iSysFileService = iSysFileService;
    }

    /**
     * 上传文件接口
     *
     * @param fileVo
     * @param multipartFile
     * @return
     */
    @PostMapping("/createFile")
    public AjaxResult createFile(@Valid FileVo fileVo, @RequestParam("file") MultipartFile multipartFile) {
        iSysFileService.createFile(fileVo, multipartFile);
        return new AjaxResult(HttpStatus.SUCCESS, "文件已上传待审核");
    }

    /**
     * 批量删除文件
     *
     * @param fileVo
     * @return
     */
    @DeleteMapping("/deleteFile")
    public AjaxResult deleteFile(@RequestBody FileVo fileVo) {
        iSysFileService.deleteFile(fileVo);
        return new AjaxResult(HttpStatus.SUCCESS, "删除成功");
    }

    /**
     * 条件查询文件(无条件为查询所有)
     */
    @GetMapping("/getFileList")
    public AjaxResult getFileList(FileVo fileVo) {
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", iSysFileService.queryAll(fileVo));
    }

    /**
     * 审核文件 0为通过 2为不通过
     *
     * @param fileVo
     * @return
     */
    @PutMapping("/auditFile")
    public AjaxResult auditFile(@RequestBody FileVo fileVo) {
        iSysFileService.auditFile(fileVo);
        return new AjaxResult(HttpStatus.SUCCESS, "审核文件成功");
    }
}
